package cn.org.timespace.easyframe.util.bean;

import java.lang.reflect.Field;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.model.permission.EfMenu;

//import com.sun.org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtils;

public class BeanUtil {

	private final static Log log = LogFactory.getLog(BeanUtil.class);

	private final static String INVALID_FIELD = "serialVersionUID";

	/**
	 * 将POJO类的属性值设为NULL
	 */
	public static void beanProperty2Null(Object obj) {
		Field[] fields = obj.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			String key = field.getName();
			if (INVALID_FIELD.equals(key))
				continue;
			try {
				String value = BeanUtils.getProperty(obj, key);
				if (value == null) {
					continue;
				}
				BeanUtils.setProperty(obj, key, null);
			} catch (Exception e) {
				if (log.isErrorEnabled())
					log.error("将Bean的属性值设置为NULL出错,原因是:" + e.getMessage());
				continue;
			}
		}
	}

	public static void main(String[] args) {
		EfMenu efMenu = new EfMenu();
		efMenu.setMenuId("aaa");
		BeanUtil.beanProperty2Null(efMenu);
		System.out.println(efMenu.getMenuId());
	}

}
